import { NextFunction, Request, Response } from "express";
import { sessionGet } from "../session/sess";

export interface AuthRequest extends Request{
    user: any,
    token:string
}

export default function auth(req:AuthRequest,res:Response,next:NextFunction) {
    if (req.path.indexOf("/login") != -1 || req.path.indexOf("/reg") != -1) {
        next()
        return
    }
    let token = req.query["token"].toString()
    let user = sessionGet(token, "user")
    if (!user) {
        res.status(403);
        res.end();
        return;
    }
    if (user) {
        req.token = token
        req.user = user
    }
    next()
}